<template>
    <el-dialog
            :title="organizeParams.title"
            :visible.sync="organizeParams.visible"
            :close-on-click-modal="false"
            width="600px"
            append-to-body>
        <el-form :model="formData" :rules="rules" :disabled="organizeParams.type === 'detail'" ref="formData" label-width="100px" class="demo-fileForm">
            <el-form-item label="组织编号：" prop="orgCode">
                <el-input v-model.trim="formData.orgCode" placeholder="请输入组织编号"></el-input>
            </el-form-item>
            <el-form-item label="组织名称：" prop="orgName">
                <el-input v-model.trim="formData.orgName" placeholder="请输入组织名称"></el-input>
            </el-form-item>
            <el-form-item label="负责人：" prop="chargePerson">
                <el-select v-model="formData.chargePerson" filterable placeholder="请选择负责人">
                    <el-option
                            v-for="(item,index) in chargePersonArr"
                            :key="index"
                            :label="item.realName"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="管理员：" prop="adminPerson">
                <el-select v-model="formData.adminPerson" filterable placeholder="请选择管理员">
                    <el-option
                            v-for="(item,index) in chargePersonArr"
                            :key="index"
                            :label="item.realName"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="上级组织：" prop="parentId">
                <el-select v-model="formData.parentId" filterable clearable placeholder="请选择">
                    <el-option
                            v-for="item in organizeArr"
                            :key="item.id"
                            :label="item.orgName"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="备注：" prop="remark">
                <el-input
                        type="textarea"
                        placeholder="请输入组织配置信息"
                        :autosize="{ minRows: 4, maxRows: 10}"
                        v-model.trim="formData.remark"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close('formData')">取 消</el-button>
            <el-button type="primary" @click="submit('formData')">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import {addOrganize, getAllUser, getOrganizeDetail, updateOrganize, getAllOrganize } from "@/api/systemManage";

    export default {
        name: 'addOrganize',
        props: {
            organizeParams: {
                type: Object
            }
        },
        data () {
            return {
                chargePersonArr: [],
                organizeArr: [],
                formData: {
                    orgCode: '',
                    orgName: '',
                    adminPerson: '',
                    chargePerson: '',
                    parentId: '',
                    remark: ''
                },
                rules: {
                    orgCode: [
                        {required: true, message: '请输入组织编号', trigger: 'blur'}
                    ],
                    orgName: [
                        {required: true, message: '请输入组织名称', trigger: 'blur'}
                    ],
                    chargePerson: [
                        {required: true, message: '请选择负责人', trigger: 'change'}
                    ],
                    adminPerson: [
                        {required: true, message: '请选择管理员', trigger: 'change'}
                    ]
                },
            }
        },
        created () {
            this.getChargePerson();
            this.getOrganizeAll();

            // console.log(this.organizeParams);
            if (this.organizeParams.id) {
                this.getDetail()
            }
        },
        methods: {
            // 获取详情
            getDetail () {
                getOrganizeDetail(this.organizeParams.id).then(res => {
                    this.formData = res || {}
                }).catch(()=> {})
            },
            // 获取负责人下拉数据
            getChargePerson () {
                getAllUser().then(res => {
                    this.chargePersonArr = res || [];
                }).catch()
            },
            // 获取上级组织下拉数据
            getOrganizeAll() {
                getAllOrganize().then(res => {
                    this.organizeArr = res || []
                }).catch(()=> {})
            },
            // 保存数据
            submit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let params = null;
                        switch (this.organizeParams.type) {
                            case 'add':
                                params = this.formData;
                                addOrganize(params).then(res => {
                                    if (res) {
                                        this.$message.success('操作成功');
                                        this.$emit('getList');
                                        this.organizeParams.visible = false;
                                    }
                                }).catch();
                                break;
                            case 'edit':
                                updateOrganize(this.organizeParams.id,this.formData).then(res => {
                                    if (res) {
                                        this.$message.success('操作成功');
                                        this.$emit('getList');
                                        this.organizeParams.visible = false;
                                    }
                                }).catch();
                                break
                        }
                    } else {
                        return false;
                    }
                });
            },
            // 关闭
            close (formName) {
                this.$refs[formName].resetFields();
                this.organizeParams.visible = false;
            }
        }
    }
</script>

<style scoped>

</style>